
package com.starshineworks.util.actions;

import com.starshineworks.entities.*;

import java.util.Map;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ActionRegister extends Action {
    
    @Override
    public boolean perform(HttpServletRequest  request, 
                           HttpServletResponse resp,
                           HttpServlet         serv, 
                           boolean             isMultiPart)
    {
        Map<String,String[]> map = request.getParameterMap();
        String name = map.get("username")[0];
        String pass = map.get("password")[0];
        List<EntityE> list = EntityE.retrieve(new UserE(name));
        if (list.isEmpty()) {
            HttpSession sess = request.getSession();
            UserE.create1(name, pass, false);
            UserE user = (UserE)EntityE.retrieve(new UserE(name)).get(0);
            sess.setAttribute("uId", user._id);
            return (user != null);
        }
        return false;
    }
    
    @Override
    public String getNextPage() {
        return "Welcome";
    }
    
    public String getError() {
        return "User already exists.";
    }

}
